package com.easy.service.impl;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.easy.bean.Order;
import com.easy.bean.Orderinfo;
import com.easy.dao.OrderDao;
import com.easy.dao.OrderinfoDao;
import com.easy.dto.OrderWithItemsDTO;
import com.easy.service.IOrderService;
import com.easy.util.Page;
import com.easy.util.PaymentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.image.BufferedImage;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class OrderService implements IOrderService {
    @Autowired
    OrderDao orderDao;

    @Autowired
    OrderinfoDao orderInfoDao;

    @Override
    public int add(Order order) {
        //设置订单创建时间
        if(order.getAddtime()==null){
            order.setAddtime(LocalDateTime.now());
        }
        //设置订单更新时间
        order.setUpdatetime(LocalDateTime.now());
        return orderDao.add(order);
    }

    @Override
    public int edit(Order order) {
        if(order.getStatus()==2){
            order.setDeliverytime(LocalDateTime.now());
        }
        if(order.getStatus()==3){
            order.setGettime(LocalDateTime.now());
        }
        if(order.getStatus()==4){
            order.setEndtime(LocalDateTime.now());
        }
        //设置订单更新时间
        order.setUpdatetime(LocalDateTime.now());
        return orderDao.edit(order);
    }

    @Override
    public int delete(int orderid, int userid) {
        return orderDao.delete(orderid, userid);
    }

    @Override
    public int del(int orderid) {
        return orderDao.del(orderid);
    }

    @Override
    public List<Order> getList(Page page, String text, int userid) {
        return orderDao.getList(page, text, userid);
    }

    @Override
    public Order getOrderById(int orderid, int userid) {
        return orderDao.getOrderById(orderid, userid);
    }

    @Override
    public int getCount(String text, int userid) {
        return orderDao.getCount(text, userid);
    }

    @Override
    public Order getOrder(Order order) {
        return orderDao.getOrder(order);
    }

    @Override
    public String payment(int orderid){
        Order order=orderDao.getOrderByID(orderid);
        if(order==null){
            throw new RuntimeException("订单不存在");
        }
        if(order.getStatus()==-1){
            throw new RuntimeException("订单已失效");
        }
        if(order.getStatus()!=0){
            throw new RuntimeException("订单已支付");
        }
        //准备支付--生成二维码
        String url=null;
        try {

            url=PaymentUtil.payment(order);
            System.out.println("url--------------------------"+url);
        }catch(Exception e){
            System.out.println("url="+url);
            throw new RuntimeException("支付服务异常");
        }
        //生成二维码图片
        BufferedImage img=QrCodeUtil.generate(url, 300, 300);
        //将图片转成Base64编码
        String qrcode=ImgUtil.toBase64(img,"png");
        System.out.println(qrcode);
        return qrcode;
    }

    @Override
    public Order getOrderByID(int orderid) {
        return orderDao.getOrderByID(orderid);
    }

    @Override
    public int paymentSuccess(HashMap map){
        //检查订单是否存在,是否是阿里后台的回调
        String code=(String)map.get("out_trade_no");
        System.out.println(code);
        Integer orderid=Integer.parseInt(code);
        System.out.println(orderid);
        return orderDao.paymentSuccess(orderid);
    }
    @Override
    public int cancelOrder(){
        return orderDao.cancleOrder();
    }

    @Override
    public List<Order> getList1(Page page, String text) {
        return orderDao.getList1(page, text);
    }

    @Override
    public int getCount1(String text) {
        return orderDao.getCount1(text);
    }

    @Override
    public int change(Order order) {
        if(order.getStatus()==2){
            order.setDeliverytime(LocalDateTime.now());
        }
        if(order.getStatus()==3){
            order.setGettime(LocalDateTime.now());
        }
        if(order.getStatus()==4){
            order.setEndtime(LocalDateTime.now());
        }
        return orderDao.change(order);
    }

    // ... 现有代码 ...

    @Override
    @Transactional
    public int createOrderWithItems(OrderWithItemsDTO orderDTO, int userId) {
        // 创建订单对象
        Order order = new Order();
        order.setUserid(userId);
        order.setMoney(orderDTO.getMoney());
        order.setStatus(0); // 设置初始状态为未支付
        order.setAddtime(LocalDateTime.now()); // 设置当前时间为添加时间

        // 添加订单并获取生成的订单ID
        orderDao.add(order);
        int orderId = order.getOrderid(); // 确保这里能获取到自动生成的ID

        // 添加订单项
        for (OrderWithItemsDTO.OrderItemDTO item : orderDTO.getItems()) {
            Orderinfo orderinfo = new Orderinfo();
            orderinfo.setOrderid(orderId);
            orderinfo.setProductid(item.getProductid());
            orderinfo.setCount(item.getCount());
            orderinfo.setProductname(item.getProductname());
            orderinfo.setPrice(item.getPrice());
            orderinfo.setImgurl(item.getImgurl());

            orderInfoDao.add(orderinfo);
        }

        return orderId; // 返回实际生成的订单ID
    }

// ... 现有代码 ...
}
